#!/usr/bin/perl -w
##
# @param $1 install home
# @param $2 delimiter
# @param $3 host
# @param $4 port
# @param $5 username
# @param $6 password
#
# -- 05/25/2009 Created by Zhixin Li
# -- Used to check mysql account status
# -- changed output from key|value to v_root|v_anonymous|v_pingmysql, 06/26/2009 modified by Zhixin Li
# -- changed subquery to single query for mysql4 does'nt support subquery 07/02/2009 modified by Zhixin Li
#  user_remote_root|user_anonymous|user_pingmysql|test_db|user_with_nopwd|host_from_anywhere|user_db_droppriv|user_mysqlpriv
use strict;
my ($MYSQL_HOME,$DELIM,$HOST,$PORT,$USER,$PASS) = @ARGV;
my $PARAM="";
$PARAM="$PARAM -h\"$HOST\"" if (defined($HOST) && $HOST ne "");
$PARAM="$PARAM -P\"$PORT\"" if (defined($PORT) && $PORT ne "");
$PARAM="$PARAM -u\"$USER\"" if (defined($USER) && $USER ne "");
$PARAM="$PARAM -p\"$PASS\"" if (defined($PASS) && $PASS ne "");
my $MYSQL="$MYSQL_HOME/bin/mysql";
my $MYSQLADMIN="$MYSQL_HOME/bin/mysqladmin";
my $SQL1 ="select distinct user from mysql.user where password='';";
my $SQL2 ="select distinct user from mysql.user where host='%';";
my $SQL3 ="select distinct user from mysql.db where Drop_priv='Y';";
my $SQL4="select count(*) root from mysql.user WHERE User='root' AND Host not in ('localhost','127.0.0.1');";
my $SQL5="select count(*) anonymous FROM mysql.user WHERE User='';";
my $SQL6="select count(*) pingmysql FROM mysql.user WHERE User='pingmysql'";
my $SQL7 ="show databases like 'test';";
my $SQL8 ="select count(*) from mysql.db where db='mysql' and user not in('root','mysqlmon');";
my $SQL9 ="select count(*) from mysql.tables_priv where db='mysql' and user not in('root','mysqlmon');";

if ($^O =~ m/MSWin32/) {
  $MYSQL =~ s/\//\\/g;
  $MYSQLADMIN =~ s/\//\\/g;
}
# Check if mysqld is alive
my $retmsg=`$MYSQLADMIN $PARAM ping 2>&1`;
chomp($retmsg);
if ( "$retmsg" ne "mysqld is alive" ) {
    print "em_error=Status${DELIM}Down\n";
    exit 1;
}
#####################################
#  v_root|v_anonymous|v_pingmysql
#####################################
# Execute command and display the result as key-value pairs
my @qry1=`$MYSQL -N -s --batch $PARAM -e "$SQL1"`;
my @qry2=`$MYSQL -N -s --batch $PARAM -e "$SQL2"`;
my @qry3=`$MYSQL -N -s --batch $PARAM -e "$SQL3"`;
my $qry4=`$MYSQL -N -s --batch $PARAM -e "$SQL4"`;
my $qry5=`$MYSQL -N -s --batch $PARAM -e "$SQL5"`;
my $qry6=`$MYSQL -N -s --batch $PARAM -e "$SQL6"`;
my $qry7=`$MYSQL -N -s --batch $PARAM -e "$SQL7"`;
my $qry8=`$MYSQL -N -s --batch $PARAM -e "$SQL8"`;
my $qry9=`$MYSQL -N -s --batch $PARAM -e "$SQL9"`;
chomp($qry4);
chomp($qry5);
chomp($qry6);
chomp($qry7);
chomp($qry8);
chomp($qry9);
my $mysqlpriv = $qry8+$qry9;
my $grep = grep(/test/,$qry4);
my $concat1 = '';
my $concat2 = '';
my $concat3 = '';
my $count;
chop(@qry1);
chop(@qry2);
chop(@qry3);
if (scalar(@qry1) gt 0) {
    for (@qry1){
        chomp($_);
        my $name = $_.':';
        if ($_ eq "") {
           $name ='anonymous:';
        }
        $concat1 .= $name;
    }
} else {
  $concat1 = 0;
}
if (scalar(@qry2) gt 0) {
   for (@qry2) {
       chomp($_);
       my $name = $_.':';
       if ($_ eq "") {
          $name ='anonymous:';
       }
       $concat2 .= $name;
   }
} else {
  $concat2 = 0;
}
if (scalar(@qry3) gt 0) {
   for (@qry3) {
       chomp($_);
       my $name = $_.':';
       if ($_ eq "") {
          $name ='anonymous:';
       }
       $concat3 .= $name;
   }
} else {
  $concat3 = 0;
}
print "em_result=$qry4${DELIM}$qry5${DELIM}$qry6${DELIM}$grep${DELIM}$concat1${DELIM}$concat2${DELIM}$concat3${DELIM}$mysqlpriv\n";
exit 0;
